package com.un.ebs.sale.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.un.ebs.core.exception.BusinessEntityNotFoundException;
import com.un.ebs.core.exception.BusinessException;
import com.un.ebs.core.util.ReportUtils;
import com.un.ebs.core.util.StringUtils;
import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.inventory.dto.InventoryNoteVo;
import com.un.ebs.sale.domain.SaleDeliverItemView;
import com.un.ebs.sale.domain.SaleDeliverNoteExt;
import com.un.ebs.sale.domain.SaleDeliverNoteItemExt;
import com.un.ebs.sale.dto.SaleDeliverNoteUpdateDto;
import com.un.ebs.sale.dto.SaleDeliverNoteVo;
import com.un.ebs.sale.service.SaleDeliverNoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * (SaleDeliverNote)表控制层
 *
 * @author liubo
 * @since 2020-08-19 12:17:10
 */
@RestController
@RequestMapping("saleDeliverNote")
@Api(value = "sale-deliver-note")
public class SaleDeliverNoteController {
    /**
     * 服务对象
     */
    @Resource
    private SaleDeliverNoteService service;

    @PostMapping("addByContract")
    @ApiOperation(value = "根据销售合同号创建装箱单")
    public WebResult<String> addByContract(@RequestParam String code) {
        return WebUtils.success(service.addByContract(code));
    }

    @PostMapping("save")
    @ApiOperation(value = "创建或修改")
    public WebResult<String> update(@RequestBody @Validated SaleDeliverNoteUpdateDto req) {
        String id = service.save(req);
        service.submit(id);
        return WebUtils.success(id);
    }

    @PostMapping("submit")
    @ApiOperation(value = "提交")
    public WebResult submit(@RequestParam String id) {
        service.submit(id);
        return WebUtils.success();
    }

    @PostMapping("submitCancel")
    @ApiOperation(value = "撤销")
    public WebResult submitCancel(@RequestParam String id) {
        service.submitCancel(id);
        return WebUtils.success();
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除")
    public WebResult delete(@RequestParam String id) {
        service.delete(id);
        return WebUtils.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "详情")
    public WebResult<SaleDeliverNoteVo> queryViewById(@PathVariable String id) {
        return WebUtils.success(service.queryViewId(id));
    }

    @GetMapping("list")
    @ApiOperation(value = "查询")
    public WebResult<IPage<SaleDeliverNoteExt>> query(@RequestParam String searchText, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.query(searchText, pageSize, pageIndex));
    }

    @GetMapping("listItem")
    @ApiOperation(value = "查询")
    public WebResult<IPage<SaleDeliverNoteItemExt>> querItem(@RequestParam String id) {
        return WebUtils.success(service.queryItemById(id));
    }

    @GetMapping("listItemQuery")
    @ApiOperation(value = "查询")
    public WebResult<IPage<SaleDeliverNoteItemExt>> querItem(@RequestParam String searchText,@RequestParam Integer pageSize,@RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryItem(searchText,pageSize,pageIndex));
    }

    @GetMapping("listItemByContract")
    @ApiOperation(value = "查询")
    public WebResult<List<SaleDeliverItemView>> queryItemByContract(@RequestParam String code) {
        return WebUtils.success(service.queryItemByContract(code));
    }

    @GetMapping("listCanDeliverNote")
    @ApiOperation(value = "查询")
    public WebResult<SaleDeliverNoteVo> queryCanDeliverList(@RequestParam String saleContractCode) {
        return WebUtils.success(service.queryCanDeliverNote(saleContractCode));
    }

    @GetMapping("print")
    @ApiOperation("打印")
    public void print(@RequestParam String id, HttpServletResponse response) {
        SaleDeliverNoteVo note = service.queryViewId(id);
        if (note == null) {
            throw new BusinessEntityNotFoundException(id, "库存业务单据数据");
        }
        ReportUtils.print(note, note.getItems(), "reports/sale_deliver_note.jrxml", response);
    }

}
